package src.tree;

import java.util.TreeMap;

/**
 * 字典树
 * 添加，查询，操作与存储的元素无关，仅与查询的单词长度有关
 */
public class Trie {
 
    private class Node{
    
        public boolean isWord;
        public TreeMap<Character, Node> next;

        public Node(boolean isWord){
            this.isWord = isWord;
            next = new TreeMap<>();
        }
        public Node(){
            this(false);
        }
    }

    private Node root;
    private int size;
    public Trie(){
        root = new Node();
        size = 0;
    }

    /**
     * 向字典树中添加一个单词
     * @param word
     */
    public void add(String word){
        Node cur = root;
        for (int i = 0; i < word.length(); i++) {
            char c = word.charAt(i);
            // 如果下个节点不存在，则创建
            if (cur.next.get(c) == null) {
                cur.next.put(c, new Node());
            }
            // 指向下个节点
            cur = cur.next.get(c);
        }
        if (! cur.isWord) {
            cur.isWord = true;
            size ++;
        }
    }
    /**
     * 从字典树中查询一个单词
     * @param word
     * @return
     */
    public boolean contains(String word) {
        Node cur = root;
        for (int i = 0; i < word.length(); i++) {
            char c = word.charAt(i);
            if (cur.next.get(c) == null) {
                return false;
            }
            cur = cur.next.get(c);
        }
        return cur.isWord;
    }

    /**
     * 查询字典树中是否存在以prefix为前缀的字符串
     * @param prefix 前缀字符串
     * @return
     */
    public boolean isPrefix(String prefix){
        Node cur = root;
        for (int i = 0; i < prefix.length(); i++) {
            char c = prefix.charAt(i);
            if (cur.next.get(c) == null) {
                return false;
            }
            cur = cur.next.get(c);
        }
        return true;
    }

    public boolean serch(String word) {
        return match(root, word, 0);
    }

    private boolean match(Node node, String word, int index){
        // 递归到底的情况
        if (index == word.length()) {
            return node.isWord;
        }
        char c = word.charAt(index);
        if (c == '.') {
            for (char nextChar : node.next.keySet()) {
                if (match(node.next.get(nextChar), word, index + 1)) {
                    return true;
                }else{
                    return false;
                }
            }
        }
        if (node.next.get(c) == null) {
            return false;
        }
        return match(node.next.get(c), word, index + 1);
    }

    public int count(String word){
        Node cur = root;
        for (int i = 0; i < word.length(); i++) {
            char c = word.charAt(i);
            if (cur.next.get(c) == null) {
                return 0;
            }
            cur = cur.next.get(c);
        }
        return prefixCount(cur);
    }

    private int prefixCount(Node node){
        int count = node.isWord ? 1 : 0;
        for (char c : node.next.keySet()) {
            count += prefixCount(node.next.get(c));
        }
        return count;
    }

    

    public int getSize() {
        return size;
    }

}